package com.xiaoyun.controller;

import com.xiaoyun.constant.Const;
import com.xiaoyun.pojo.Orders;
import com.xiaoyun.pojo.Users;
import com.xiaoyun.service.center.MyOrderService;
import com.xiaoyun.utils.RedisUtil;
import com.xiaoyun.utils.XYJSONResult;
import com.xiaoyun.vo.UsersVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.UUID;

@RequiredArgsConstructor
public class BaseController {
    @Autowired
    public MyOrderService myOrderService;
    @Autowired
    public RedisUtil redisUtil;



    public XYJSONResult checkUserOrder(String userId, String orderId) {
        Orders orders = myOrderService.queryMyOrder(userId, orderId);
        if (orders == null) {
            return XYJSONResult.errorMsg("订单不存在");
        }
        return XYJSONResult.ok(orders);
    }

    public UsersVO conventUsersVO(Users userResult) {
        //生成用户token
        String uniqueToken = UUID.randomUUID().toString().trim();
        redisUtil.setKey(Const.REDIS_USER_TOKEN + ":" + userResult.getId(), uniqueToken);
        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(userResult, usersVO);
        usersVO.setUserUniqueToken(uniqueToken);
        return usersVO;
    }
}
